package com.ld.admin.controller.system.filelistener;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.filelistener.SysFilelistenerModel;
import com.ld.admin.service.system.filelistener.SysFileLinstenerService;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.PathUtil;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 文件监听器
 * 
 * @ClassName SysFilelistenerController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年01月18日 14:41:06
 *
 */
@Api(tags = "文件监听器")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "filelistener.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemSysFilelistenerController")
@RequestMapping("${shieldsb.adminPath}/system/filelistener")
public class SysFilelistenerController extends AdminBasicController<SysFilelistenerModel> {
    @Autowired
    protected SysFileLinstenerService service;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/system/filelistener/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/system/filelistener/sysfilelistener_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = basicService.getSearchQueryModel(request);
        service.getPageBean(request, getModelClass(), queryModel);
        return "a/system/filelistener/sysfilelistener_list";
    }

    @ApiOperation(value = "文件监听器展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        service.getOne(request, getModelClass(), id);
        return "a/system/filelistener/sysfilelistener_show";
    }

    @ApiOperation(value = "跳转到添加/修改文件监听器", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        request.setAttribute("projectPath", PathUtil.getClassRootPath());
        basicService.getOne(request, getModelClass(), id);
        return "a/system/filelistener/sysfilelistener_update";
    }

    @ApiOperation(value = "添加/修改文件监听器", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid SysFilelistenerModel model) {
//        model.setInterval(null);
        Result result = service.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除文件监听器", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = service.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "启动/停止定时任务", notes = "")
    @RequestMapping(path = "/startOrStop", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object startOrStop() {
        Result result = service.startOrStop(getModelClass(), getLong("id"), getInt("isstart"));
        return Web.Response.result(result);
    }

}